import { createPinia } from 'pinia'

const pinia = createPinia()

// 持久化插件
pinia.use(({ store }) => {
  // 从 localStorage 恢复状态
  const stored = localStorage.getItem(`pinia-${store.$id}`)
  if (stored) {
    try {
      store.$patch(JSON.parse(stored))
    } catch (error) {
      console.error('恢复状态失败:', error)
    }
  }

  // 监听状态变化并保存
  store.$subscribe((mutation, state) => {
    localStorage.setItem(`pinia-${store.$id}`, JSON.stringify(state))
  })
})

export default pinia

// 导出所有 store
export { useAuthStore } from './auth' 